package com.hzt.business.controller;

import com.hzt.business.service.CustomerService;
import com.hzt.system.common.Constant;
import com.hzt.system.common.ResultObj;
import com.hzt.business.domain.Customer;
import com.hzt.business.vo.CustomerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.ArrayList;
import java.util.List;

/**
* 
*
* @Author:hzt
* @Date:6:52 下午 2020/7/25
*/
@RequestMapping("api/customer")
@RestController
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("loadAllCustomer")
    public Object loadAllCustomer(CustomerVo customerVo){
        return this.customerService.queryAllCustomer(customerVo);
    }


    @RequestMapping("addCustomer")
    public ResultObj addCustomer(Customer customer){

        try {
            customer.setAvailable(Constant.AVAILABLE_TRUE);
            customerService.saveCustomer(customer);
            return  ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultObj.ADD_SUCCESS;
        }
    }


    @RequestMapping("updateCustomer")
    public ResultObj updateCustomer(Customer customer){
        try {
            customerService.updateCustomer(customer);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }


    @RequestMapping("deleteCustomer")
    public ResultObj deleteCustomer(Customer customer){
        try {
            customerService.removeById(customer.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    //批量删除
    @RequestMapping("batchDeleteCustomer")
    public ResultObj batchDeleteCustomer(Integer[] ids){
        try {
            List<Integer> listIds=new ArrayList<>();
            for (Integer id : ids) {
                listIds.add(id);
            }
            customerService.removeByIds(listIds);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 查询所有可用的客户
     */
    @GetMapping("getAllAvailableCustomer")
    public Object getAllAvailableCustomer(){
        return this.customerService.getAllAvailableCustomer();
    }


}
